import Controller from "../../../../mvc/Controller";
import createInstance from "../../../../common/utils/createInstance";

export default class RegisterViewController extends Controller {

    constructor(cmd) {
        super(cmd);
    }

    execute(data = null, sponsor = null) {

        if (!data.viewCls) {
            throw new Error('注册视图管理类不能为空！');
        }
        if (!data.vcCls) {
            throw new Error('注册视图类不能为空！');
        }

        if (data.name && this.retrieveView(data.name)) {
            this.removeView(data.name);
        }

        let vc = createInstance(data.vcCls, data.vcParameters);
        if (data.vcProperties) {
            Object.keys(data.vcProperties).map(key => {
                if (vc[key]) {
                    vc[key] = data.vcProperties[key];
                }
            });
        }
        this.registerView(data.name, data.viewCls, vc);
    }
}